const crypto = require('crypto');
const {defaultParams,DBKEY,secret}= require('./config')


function uuid() {
  const chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".split(
    ""
  );
  const uuidAns = [];
  const radixFoo = chars.length;
  for (let i = 0; i < 40; i += 1) {
    uuidAns[i] = chars[0 | (Math.random() * radixFoo)];
  }

  return uuidAns.join("");
}
function signString(query) {
  return [
    'client_id', 'code', 'response_type', 'scope', 'timestamp',
  ].map(key => `${key}=${encodeURIComponent(query[key] || '')}`).join('&');
}

function sign(signString, secret) {
  return crypto.createHmac('sha1', secret)
    .update(signString)
    .digest()
    .toString('base64');
}

let code = uuid();
const sString = signString(Object.assign({
  code
}, defaultParams));
const sSign = sign(sString,secret)

const authorizeUrl = `https://www.yuque.com/oauth2/authorize?${sString}&sign=${sSign}`;

exports.DBKEY = DBKEY;
exports.authorizeUrl = authorizeUrl;
exports.loginConfig = {
  ...defaultParams,
  code,
}

